package com.zm.damai.damaiindex.web;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.zm.damai.damaiindex.web.feign.OtherAction;
import com.zm.damai.damaiindex.web.feign.UserAction;
import com.zm.damai.damaiindex.web.feign.impl.OtherActionImpl;
import com.zm.damaicommon.Result;
import org.apache.http.HttpResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RefreshScope
@RestController
public class IndexDo {
    @RequestMapping("index")
    String index(HttpServletRequest request){
        return "index:" + request.getServerPort();
    }

    @Autowired
    RestTemplate restTemplate;
    @HystrixCommand(fallbackMethod = "userHystrix")
    @RequestMapping("user")
    String user(){
       return restTemplate.getForObject("http://damai-user/user",
                String.class);
    }

    String userHystrix(){
        return "服务降级:user";
    }

    @Autowired
    UserAction userAction;
    @RequestMapping("userByFeign")
    String userByFeign(){
        return userAction.way();
    }

    @RequestMapping("hello")
    String hello(@RequestParam("name") String name){
        return userAction.hello(name);
    }

    @RequestMapping("add")
    int add(@RequestParam("x") int x,@RequestParam("y") int y){
        return userAction.add(x,y);
    }

    @RequestMapping("sub/{x}/{y}")
    int sub(@PathVariable int x, @PathVariable int y){
        return userAction.sub(x,y);
    }

    @RequestMapping("result")
    Result sub(Result result){
        return userAction.result(result);
    }


    @RequestMapping("other")
    String other(){
        return restTemplate.getForObject("http://damai-other/other",
                String.class);
    }

    @Autowired
    OtherAction otherAction;

    @Autowired
    @Qualifier("otherActionImpl")
    OtherActionImpl otherActionImpl;
    @RequestMapping("otherByFeign")
    String otherByFeign(){
        return otherAction.way();
    }

    @RequestMapping("hot")
    List<Map<String, Object>> hot(){
        List<Map<String,Object>> list = otherAction.hot();
        otherActionImpl.setHotsCache(list);
        return list;
    }

    @RequestMapping("headers")
    Map<String ,Object> headers(@RequestHeader("Cookie") String cookie ,@RequestHeader("Host") String host){
        HashMap<String,Object> ret = new HashMap<>();
        ret.put("Cookie",cookie);
        ret.put("Host",host);
        return ret;
    }

    @RequestMapping("session")
    String sessdion(HttpSession session, HttpServletRequest request,HttpServletResponse response){
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies) {
            System.out.println("cookie.getName() = " + cookie.getName());
            System.out.println("cookie.getValue() = " + cookie.getValue());
            if (cookie.getName().equals("aaa")){
                session.setAttribute(cookie.getName(),cookie.getValue());
            }
        }
        Cookie cookie = new Cookie("name","张三");
        cookie.setMaxAge(60);
        response.addCookie(cookie);
        return session.getId() + "," + session.getAttribute("aaa");
    }

    @RequestMapping("aaa")
    String aaa(@RequestParam int a,HttpServletRequest request){

        if(a == 1){
            HttpSession session = request.getSession();
            System.out.println("session.getId() = " + session.getId());
            return session.getId();
        }

        return "aaa";
    }
    @Value("${myname}")
    String myname;

    @RequestMapping("myname")
    public String myname(){
        return myname;
    }




}
